<template>
  <div class="tab-control">
    <div class="tab-control-item"
         v-for="(item,index) in titles"
         :key="index"
         @click="itemClick(index)"
         :class="{active:index==currentIndex}">
      <span>{{ item }}</span>
    </div>
  </div>
</template>

<script>
import {ref} from 'vue'

export default {
  name: "TabControl",
  props: {
    titles: {
      type: Array,
      /*防止数据加载延时的情况下，页面报错*/
      default() {
        return []
      }
    }
  },
  setup(props, {emit}) {
    let currentIndex = ref(0)
    const itemClick = (index) => {
      currentIndex.value = index
      /*子组件向父组件传递*/
      emit('tabClick', index)
    }
    return {
      currentIndex,
      itemClick
    }
  }
}
</script>

<style scoped lang="scss">
.tab-control {
  display: flex;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  background-color: #FFFFFF;
  width: 100%;
  /*固定住某一个位置*/
  position: sticky;
  top: 44px;
  z-index: 10;

  .tab-control-item {
    flex: 1;

    span {
      padding: 6px;
    }
  }

  .active {
    color: var(--color-high-text);

    span {
      border-bottom: 3px solid var(--color-high-text);
    }
  }
}
</style>
